package com.echo.register.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.register.enums.RefundStatusEnum;
import com.echo.register.model.order.PaymentInfo;
import com.echo.register.model.order.RefundInfo;
import com.echo.register.order.mapper.RefundInfoMapper;
import com.echo.register.order.service.RefundInfoService;
import org.springframework.stereotype.Service;

/**
 * @ClassName: RefundInfoServiceImpl
 * @Package: com.echo.register.order.service.impl
 * @Description:
 * @Author 谢同学
 * @Create 2024/1/10 11:30
 * @Version 1.0
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {


    /**
     * 保存退款记录
     *
     * @param paymentInfo 退款参数
     * @return 保存的记录
     */
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        RefundInfo refundInfo = baseMapper.selectOne(new LambdaQueryWrapper<RefundInfo>()
                .eq(RefundInfo::getOrderId, paymentInfo.getOrderId()).eq(RefundInfo::getPaymentType, paymentInfo.getPaymentType()));
        if (refundInfo == null) {
            // 保存交易记录
            refundInfo = new RefundInfo();
            refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
            refundInfo.setOrderId(paymentInfo.getOrderId());
            refundInfo.setPaymentType(paymentInfo.getPaymentType());
            refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
            refundInfo.setSubject(paymentInfo.getSubject());
            refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus()); // 1 退款中

            baseMapper.insert(refundInfo);
        }
        return refundInfo;
    }
}
